import Vue from 'vue'
import VueRouter from 'vue-router'
const web = (name) => () => import(`@/views/web/${name}`);
const mobile = (name) => () => import(`@/views/mobile/${name}`);

const both=brower.isMobile?mobile:web;
Vue.use(VueRouter)

const routes = [
  {
    path: '',
    redirect:'/index'
  },
  {
    path: '/login',
    name: 'login',
    component: () => both('login.vue')
  },
  {
    path: '/index',
    meta:{
      requireAuth:true
    },
    name: 'index',
    component: () => both('index.vue')
  },
  {
    path: '/news',
    name: 'news',
    component: () => both('news.vue')
  },
  {
    path: '/newsDetails',
    name: 'newsDetails',
    component: () => both('newsDetails.vue')
  },
  {
    path: '/laws',
    name: 'laws',
    component: () => both('laws.vue')
  },
  {
    path: '/lawClass',
    name: 'lawClass',
    component: () => both('lawClass.vue')
  },
  {
    path: '/classDetails',
    name: 'classDetails',
    component: () => both('classDetails.vue')
  },
  {
    path: '/regulations',
    name: 'regulations',
    component: () => both('regulations.vue')
  },
  {
    path: '/opinionNews',
    name: 'opinionNews',
    component: () => both('opinionNews.vue')
  },
  {
    path: '/opinionNewsDetails',
    name: 'opinionNewsDetails',
    component: () => both('opinionNewsDetails.vue')
  },
  {
    path: '/opinionNews_weekDetails',
    name: 'opinionNews_weekDetails',
    component: () => both('opinionNews_weekDetails.vue')
  },
  {
    path: '/videolist',
    name: 'videolist',
    component: () => both('videolist.vue')
  },
  {
    path: '/examList',
    name: 'examList',
    component: () => both('examList.vue')
  },
  {
    path: '/examInfo',
    name: 'examInfo',
    component: () => both('examInfo.vue')
  },

  {
    path: '/toplist',
    name: 'toplist',
    component: () => both('toplist.vue')
  },
  {
    path: '/recordlist',
    name: 'recordlist',
    component: () => both('recordlist.vue')
  },

  {
    path: '/examGoing',
    name: 'examGoing',
    component: () => both('examGoing.vue')
  },
  {
    path: '/examResult',
    name: 'examResult',
    component: () => both('examResult.vue')
  },

  {
    path: '/answerlist',
    name: 'answerlist',
    component: () => both('answerlist.vue')
  },  
]

const router = new VueRouter({
  base:'/',//此处为打包后的文件夹名
  routes,
  mode:'history'
})

export default router

router.beforeEach((to, from, next) => {
  // console.log('topath=====>',to.path)
  let customertoken = localStorage.getItem('customertoken')
    // console.log('customertoken=====>',customertoken)
    if (!customertoken && to.path !== '/login') {
      // 若本地token不存在,则任意路由跳转的时候,重定向至login 登陆页面
      next('/login')
    } else {
      next()
    }
})

